package com.online.teaching.module.teacher.resource.controller;

import com.online.teaching.module.teacher.resource.service.TeacherResourceService;
import com.online.teaching.utils.Result;
import com.online.teaching.utils.StatusCode;
import com.online.teaching.utils.TokenDecode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName : StudentBlogController  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:20  //时间
 */
@Api(tags ="老师操作课件模块")
@RestController
public class TeacherResourceController {

    @Autowired
    private TeacherResourceService teacherResourceService;

    @Autowired
    private TokenDecode tokenDecode;

    @ApiOperation(value = "老师添加文件", notes = "老师添加文件")
    @PostMapping("teacher/resource/addFile")
    public Result addFile(@RequestParam("file") @ApiParam("文件") MultipartFile file,@ApiParam("teaches_id") @RequestParam("teacherId") Integer teacherId,@RequestParam(value = "parentId",required = false) Integer parentId)
    {
        Integer userId=Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = teacherResourceService.addFile(file, teacherId, parentId, userId);
        return i==1?new Result(true, StatusCode.OK,"添加文件成功"):new Result(true,StatusCode.ERROR,"添加文件失败");
    }

    @ApiOperation(value = "老师添加文件", notes = "老师添加文件")
    @PostMapping("teacher/resource/addDirectory")
    public Result addDirectory(@RequestParam("directoryName")@ApiParam("目录名称") String directoryName,@ApiParam("teaches_id") @RequestParam("teacherId") Integer teacherId,@ApiParam("其父级的目录名称如果为空就不用传过来") @RequestParam(value = "parentId",required = false) Integer parentId)
    {
        Integer userId=Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = teacherResourceService.addDirectory(directoryName, teacherId, parentId, userId);
        return i==1?new Result(true, StatusCode.OK,"添加目录成功"):new Result(true,StatusCode.ERROR,"添加目录失败");
    }


    @ApiOperation(value = "老师删除文件或者目录", notes = "老师删除文件或者目录")
    @PostMapping("teacher/resource/deleteResource")
    public Result deleteResource(@ApiParam("对应的目录或者文件的id") @RequestParam("id") Integer id)
    {
        int i = teacherResourceService.deleteResource(id);
        return i==1?new Result(true, StatusCode.OK,"删除成功"):new Result(true,StatusCode.ERROR,"删除失败");
    }

}
